package summarizer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

import summarizer.newversion.Summarizer;
import thesis.DataUtil;
import thesis.FSModule;
import thesis.InfoUnit;
import thesis.DataObject;
import thesis.Summary;

public class PinakiSummarizer extends Summarizer {
	private final double alpha;
	private final double beta;
	private final double gamma;
	private final double[][] probs;
	private HashMap<Long, DataObject> memoryTweets = new HashMap<Long, DataObject>();

	public PinakiSummarizer(final double[][] probs, double alpha, double beta,
			double gamma) {
		this.alpha = alpha;
		this.beta = beta;
		this.gamma = gamma;
		this.probs = probs;
	}

	private void greedyProcess(Summary summary, int K,
			HashMap<Integer, Integer> info2Tweet,
			ArrayList<ArrayList<Integer>> tweet2Info) {
		// compute gain for each info unit
		double currDiv = 1;
		double currCov = 0;
		while (summary.size() < K) {
			DataObject bestT = null;
			double bestScore = -1;
			for (DataObject t : memoryTweets.values()) {
				double div = currDiv;
				for (DataObject tInS : summary.getMemoryTweets()) {
					double dist = DataUtil.dist(tInS, t);
					if (dist < div) {
						div = dist;
					}
				}
				t.setDiv(div);
				double cov = 0;
				ArrayList<Integer> infos = tweet2Info.get(t.getInternId());
				for (Integer info : infos) {
					Integer numTweet = info2Tweet.get(info);
					cov += numTweet == null ? 0 : numTweet;
				}
				t.setCov(currCov + cov);
				double score = alpha * t.getQuality() + beta * t.getDiv()
						+ gamma * t.getCov();
				if (score >= bestScore) {
					bestScore = score;
					bestT = t;
				}
			}
			summary.addMemoryTweet(bestT);
			memoryTweets.remove(bestT.getDbId());
			for (Integer info : tweet2Info.get(bestT.getInternId())) {
				info2Tweet.remove(info);
			}
			currDiv = bestT.getDiv();
			currCov = bestT.getCov();
		}
	}

	@Override
	public Summary computeSummary(int summaryDimension, int numberOfPages,
			HashMap<Long, DataObject> tweets, int dimSize) {
		this.memoryTweets = tweets;
		// construct info2tweet map
		HashMap<Integer, Integer> info2Tweet = new HashMap<Integer, Integer>();
		for (int i = 0; i < probs[0].length; i++) {
			info2Tweet.put(i, 0);
		}
		// construct tweet2info map
		ArrayList<ArrayList<Integer>> tweet2Info = new ArrayList<ArrayList<Integer>>();
		for (int i = 0; i < probs.length; i++) {
			ArrayList<Integer> tmp = new ArrayList<Integer>();
			for (int j = 0; j < probs[i].length; j++) {
				if (probs[i][j] > 0.5) {
					tmp.add(j);
					info2Tweet.put(j, info2Tweet.get(j) + 1);
				}
			}
			tweet2Info.add(tmp);
		}
		Summary summary = new Summary();
		greedyProcess(summary, summaryDimension, info2Tweet, tweet2Info);
		return summary;
	}

	public String toString() {
		return "Pinaki Summarizer";
	}

	@Override
	public long getExecutionTime() {
		// TODO Auto-generated method stub
		return 0;
	}
}
